package org.tpdata.api.cdss.homepage;

import business.cdss.GetPatientList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.SetUpTearDown1;
import exceptions.TestNGRetry;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utils.Contants;

import java.io.FileNotFoundException;

import static common.TestStep.requestAndRespondBody;

public class getPatientInfo extends SetUpTearDown1 {

    Logger log = Logger.getLogger(getPatientInfo.class);

    @Story("数据库患者列表-获取患者详情")
    @Parameters({"uri","i"})
    @Test(retryAnalyzer = TestNGRetry.class,description = "数据库患者列表点击单个患者的详情按钮，获取患者详情资料（包括基本信息，诊断，临床表现，原始病例）")

    public void runCase(String uri, int i) throws FileNotFoundException, ConfigurationException, YamlException {
        GetPatientList getPatientList = new GetPatientList();
        JSONArray jsonArray = getPatientList.getlist();
        JSONObject obj1 = jsonArray.getJSONObject(i);
        String inpatientNo = obj1.getString("INPATIENT_NO");
        String hospitalCode = obj1.getString("HOSPITAL_CODE");
        JSONObject json = new JSONObject();
        Data data = envSetUp();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("inpatientNo", inpatientNo);
        json.put("hospitalCode", hospitalCode);
        log.info(json.toString());

        Response response = RunCaseJson.runCase(json, "get", uri);
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);

        JSONObject obj = JSON.parseObject(jsonStr);
        JSONObject object = obj.getJSONObject("responseData");
        String url = RestAssured.baseURI + ":" + RestAssured.port + "/" + uri;
        requestAndRespondBody(url, json, jsonStr);

        //判断ojbect是否包含以下keys {dwDiagnosis,dwLab,dwReport,dwSmoke,dwSymptom,sourceMap}
        boolean flag;
        if (object.containsKey("dwDiagnosis") && object.containsKey("dwLab") && object.containsKey("dwReport") &&
                object.containsKey("dwSmoke") && object.containsKey("dwSymptom") && object.containsKey("sourceMap")) {
            flag = true;
        }else {
            flag = false;
        }

        Assert.assertEquals(flag, Contants.SUCCESS);
    }

}
